<?php

/*/google recaptcha
require_once('./includes/recaptchalib.php');
$public_key = '6Lc_aNESAAAAAMML5MCrtOq6eFKaIXXW6p3SK6TX';
$private_key = '6Lc_aNESAAAAABNLCV1yH1JK2B23BHmklvhdrrGz';
//--------------*/

if (isset($_POST['addBtn'])) { 
	$errors = array();
	
	$username = trim($_POST['username']);
	$perm = $_POST['perm'];
	$password = trim($_POST['pwd']);
	$retyped = trim($_POST['conf_pwd']);
	
	// client
	$company = trim($_POST['company']);
	
	// Staff
	$field = $_POST['field'];
	$exp = $_POST['exp'];
	$edu = $_POST['edu'];
	$sal = $_POST['sal'];
	$zip = trim($_POST['zip']);
	
	$email = trim($_POST['email']);
	$validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
	if (!$validemail) {
		$errors[] = 'Invalid Email';
	}
	
	$fname = trim($_POST['fname']);
	$lname = trim($_POST['lname']);
	$phone = trim($_POST['phone']);
	
	require_once ('./classes/Ja1/checkpassword.php');
	$checkpwd = new Ja1_CheckPassword($password);
	$checkpwd->requireMixedCase();
	$checkpwd->requireNumbers(2);
	$checkpwd->requireSymbols();
	$passwordOK = $checkpwd->check();
	if (!$passwordOK) {
		$errors = array_merge($errors, $checkpwd->getErrors());
	}
	if ($password != $retyped) {
		$errors[] = "Your passwords don't match.";
	}
	
	// create a salt using current timestamp
	$salt = time();
	// encrypt the password and salt
	$pwd = sha1($password . $salt);
	
	$MinChars = 1;
	if (strlen($username) < $MinChars || strlen($fname) < $MinChars || strlen($lname) < $MinChars || strlen($phone) < 10) {
		$errors[] = "A field was left empty or has an invalid value.";
	}
	
	if ($perm == 2) {
		if (strlen($company) < $MinChars) {
			$errors[] = "Must provide your companies name.";	
		}
	}
	elseif ($perm == 3) {
		if ($field == "choose") {
			$errors[] = "You must select a career field.";	
		}
		if ($exp == "choose") {
			$errors[] = "You must provide your years work experience.";	
		}
		if ($edu == "0" || $edu == 0) {
			$errors[] = "You must provide you education level.";	
		}
		if ($sal == "0" || $sal == 0) {
			$errors[] = "You must select a salary range.";	
		}
		if (strlen($zip) < 5) {
			$errors[] = "Invalid zip code.";	
		}
	}
	
	/*/Check google recaptcha
	$response = recaptcha_check_answer($private_key, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
	if (!$response->is_valid) {
		$recaptchaerrors['recaptcha'] = true;
		$errors[] = 'reCAPTCHA did not match.';
	}
	//---------------------*/
	

	// If no errors add user to db	
	if (!$errors) {		
		
		$sql = 'INSERT INTO user (username, pwd, salt, perm, fname, lname, phone, email) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
		$stmt = $dbc->stmt_init();
		$stmt = $dbc->prepare($sql);
		$stmt->bind_param('ssssssss', $username, $pwd, $salt, $perm, $fname, $lname, $phone, $email);
		$stmt->execute();
		if ($stmt->affected_rows == 1) {
			
			$_SESSION['userId'] = $dbc->insert_id;
			$_SESSION['user'] = $username;
			$_SESSION['perm'] = $perm;
			
			?>
				<script type="text/javascript">refreshNav();</script>
			<?php	
						
			$success = "</h3>Account successfully created.</h3><br /><br />
			<h2>Account Number: ".$_SESSION['userId']."</h2><br /><br />
			This number can be used to locate and identify you.
			";
			
			$empty = "";
			$pic = "images/users/default.jpg";
			// if creating a client account
			if ($perm == 2 || $perm == "2") {
				$sql2 = 'INSERT INTO client (userId, company) VALUES (?, ?)';
				$stmt2 = $dbc->stmt_init();
				$stmt2 = $dbc->prepare($sql2);
				$stmt2->bind_param('is', $_SESSION['userId'], $company);
				$stmt2->execute();
				$stmt2->close();
			}
			
			// if creating a staff account
			elseif ($perm == 3 || $perm == "3") {
				$sql2 = 'INSERT INTO staff (userId, pic, zip, work, exp, edu, sal, about, jobId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
				$stmt2 = $dbc->stmt_init();
				$stmt2 = $dbc->prepare($sql2);
				$stmt2->bind_param('issssssss', $_SESSION['userId'], $pic, $zip, $field, $exp, $edu, $sal, $empty, $empty);
				$stmt2->execute();
				$stmt2->close();
			}
		}
		elseif ($stmt->errno == 1062) {
			$errors[] = "That username has already been taken.";
		}
		else {
			$errors[] = 'Sorry, there was a problem with the database.';
		}
		
		$stmt->close();		
	}
} 
?>

<?php 
if (isset($success)) {
	echo '<div style="text-align:center;width:1180px;float:left;">';
	echo $success;
	echo '</div>';
}

	

if (!isset($success)) {
?>
	<form id="techForm" method="post" action="">
	
    <div style="text-align:center;width:1180px;float:left;">
    <br /><h2>Sign Up Form</h2><br /><br />
    <!-- Client or Staff -->
    <?php
	if (isset($errors) && !empty($errors)) {
		foreach ($errors as $error) {
			echo "<font color='red'>$error</font><br />";
		}
		echo "<br />";
	}
	?>
    
    
    <div style="float:left;width:525px;text-align:right; line-height:19px;">
		<label for="username">USERNAME:&nbsp;</label><br /><br />
        <label for="pwd">PASSWORD:&nbsp;</label><br /><br />
    	<label for="conf_pwd">RETYPE PASSWORD:&nbsp;</label><br /><br /><br />
        <label for="fname">FIRST NAME:&nbsp;</label><br /><br />
        <label for="lname">LAST NAME:&nbsp;</label><br /><br />
        <label for="phone">PHONE:&nbsp;</label><br /><br />
        <label for="email">EMAIL:&nbsp;</label>
    </div>
    
    <div style="float:left;text-align:left;line-height:20px;">
    	<input name="username" id="username" type="text" <?php if (isset($_POST['addBtn'])) { echo 'value="'.$username.'"'; } ?> onclick='value=""'><br /><br />
		<input name="pwd" id="pwd" type="password" <?php if ((isset($_POST['addBtn'])) && ($password == $retyped)) { echo 'value="'.$password.'"'; } ?> onclick='value=""'><br /><br />
		<input name="conf_pwd" id="conf_pwd" type="password" <?php if ((isset($_POST['addBtn'])) && ($password == $retyped)) { echo 'value="'.$retyped.'"'; } ?> onclick='value=""'><br /><br /><br />
        <input name="fname" id="fname" type="text" <?php if (isset($_POST['addBtn'])) { echo 'value="'.$fname.'"'; } ?> onclick='value=""'><br /><br />
        <input name="lname" id="lname" type="text" <?php if (isset($_POST['addBtn'])) { echo 'value="'.$lname.'"'; } ?> onclick='value=""'><br /><br />
        <input name="phone" id="phone" type="text" <?php if (isset($_POST['addBtn'])) { echo 'value="'.$phone.'"'; } ?> onclick='value=""'><br /><br />
        <input name="email" id="email" type="text" <?php if (isset($_POST['addBtn'])) { echo 'value="'.$email.'"'; } ?> onclick='value=""'><br /><br />
        <br />
    </div>
    
    <div style="width:1180px;float:left;"><div style="float:left;margin-left:425px;text-align:left;">
    	<input type="radio" name="perm" id="perm1" class="perm1" value="1" <?php if (isset($perm) && $perm == "1") { echo 'checked'; } ?> />&nbsp;Admin - Here simply so the instructor can easily see all parts.<br />
    	<input type="radio" name="perm" id="perm2" class="perm2" value="2" <?php if (isset($perm) && $perm == "2") { echo 'checked'; } ?> />&nbsp;Client - Looking to hire.<br />
   		<input type="radio" name="perm" id="perm3" class="perm3" value="3" <?php if ((isset($perm) && $perm == "3") || !isset($perm)) { echo 'checked'; } ?> />&nbsp;Staff - Looking for work.<br /><br /><br />
    </div></div>
    
    <div id="client_form" <?php if (isset($perm) && $perm == "2") { echo 'class="form_unhide"'; } else { echo 'class="hidden"'; } ?>>
    	<div style="float:left;width:525px;text-align:right; line-height:19px;">
        	<label for="company">COMPANY:&nbsp;</label>
        </div>
        <div style="float:left;text-align:left;line-height:20px;">
        	<input name="company" id="company" type="text" <?php if (isset($_POST['addBtn'])) { echo 'value="'.$company.'"'; } ?> onclick='value=""'><br /><br />
        </div>
    </div>
    
    <div id="staff_form" <?php if ((isset($perm) && $perm == "3") || !isset($perm)) { echo 'class="form_unhide"'; } else { echo 'class="hidden"'; } ?>>
    	<div style="float:left;width:525px;text-align:right; line-height:19px;">
        	<label for="work">FIELD:&nbsp;</label><br /><br />
            <label for="exp">EXPERIANCE:&nbsp;</label><br /><br />
            <label for="edu">EDUCATION:&nbsp;</label><br /><br />
            <label for="sal">SALARY:&nbsp;</label><br /><br />
            <label for="zip">ZIPCODE:&nbsp;</label><br /><br />
        </div>
        <div style="float:left;text-align:left;line-height:20px;">
        	<?php include('./includes/field.php');	?><br /><br />
            <select name="exp">
            	<option value="choose" <?php if (!isset($exp)) { echo 'selected'; } ?>>Choose</option>
				<option value="0"<?php if(isset($exp) && $exp == '0'){echo 'selected';}?>>None</option>
                <option value="1"<?php if(isset($exp) && $exp == '1'){echo 'selected';}?>>1 Year</option>
                <option value="2"<?php if(isset($exp) && $exp == '2'){echo 'selected';}?>>2 Years</option>
                <option value="3"<?php if(isset($exp) && $exp == '3'){echo 'selected';}?>>3 Years</option>
                <option value="4"<?php if(isset($exp) && $exp == '4'){echo 'selected';}?>>4 Years</option>
                <option value="5"<?php if(isset($exp) && $exp == '5'){echo 'selected';}?>>5 Years</option>
                <option value="6"<?php if(isset($exp) && $exp == '6'){echo 'selected';}?>>6 Years</option>
                <option value="7"<?php if(isset($exp) && $exp == '7'){echo 'selected';}?>>7 Years</option>
                <option value="8"<?php if(isset($exp) && $exp == '8'){echo 'selected';}?>>8 Years</option>
                <option value="9"<?php if(isset($exp) && $exp == '9'){echo 'selected';}?>>9 Years</option>
                <option value="10"<?php if(isset($exp) && $exp == '10'){echo 'selected';}?>>10 Years</option>
                <option value="11"<?php if(isset($exp) && $exp == '11'){echo 'selected';}?>>11 Years</option>
                <option value="12"<?php if(isset($exp) && $exp == '12'){echo 'selected';}?>>12 Years</option>
                <option value="13"<?php if(isset($exp) && $exp == '13'){echo 'selected';}?>>13 Years</option>
                <option value="14"<?php if(isset($exp) && $exp == '14'){echo 'selected';}?>>14 Years</option>
                <option value="15"<?php if(isset($exp) && $exp == '15'){echo 'selected';}?>>15+ Years</option>
            </select><br /><br />
            <select name="edu">
            	<option value="0" <?php if (!isset($edu)) { echo 'selected'; } ?>>Choose</option>
				<option value="1"<?php if(isset($edu) && $edu == '1'){echo 'selected';}?>>High School</option>
                <option value="2"<?php if(isset($edu) && $edu == '2'){echo 'selected';}?>>Associate Degree</option>
                <option value="3"<?php if(isset($edu) && $edu == '3'){echo 'selected';}?>>Bachelor's Degree</option>
                <option value="4"<?php if(isset($edu) && $edu == '4'){echo 'selected';}?>>Advanced Degree</option>
            </select><br /><br />
           	<select name="sal">
            	<option value="0" <?php if (!isset($sal)) { echo 'selected'; }?>>Choose</option>
                <option value="1"<?php if(isset($sal) && $sal == '1'){echo 'selected';}?>>$30,000.00+</option>
                <option value="2"<?php if(isset($sal) && $sal == '2'){echo 'selected';}?>>$40,000.00+</option>
                <option value="3"<?php if(isset($sal) && $sal == '3'){echo 'selected';}?>>$50,000.00+</option>
                <option value="4"<?php if(isset($sal) && $sal == '4'){echo 'selected';}?>>$60,000.00+</option>
                <option value="5"<?php if(isset($sal) && $sal == '5'){echo 'selected';}?>>$70,000.00+</option>
                <option value="6"<?php if(isset($sal) && $sal == '6'){echo 'selected';}?>>$80,000.00+</option>
                <option value="7"<?php if(isset($sal) && $sal == '7'){echo 'selected';}?>>$90,000.00+</option>
                <option value="8"<?php if(isset($sal) && $sal == '8'){echo 'selected';}?>>$100,000.00+</option>
                <option value="9"<?php if(isset($sal) && $sal == '9'){echo 'selected';}?>>$125,000.00+</option>
                <option value="10"<?php if(isset($sal) && $sal == '10'){echo 'selected';}?>>$150,000.00+</option>
            </select><br /><br />
             <input name="zip" id="zip" type="text" <?php if (isset($_POST['addBtn'])) { echo 'value="'.$zip.'"'; } ?> onclick='value=""'><br /><br />
        </div>
    </div>
    
    
    
    <div style="width:1180px;float:left;text-align:center;">
		<!--<div style="width:325px;text-align:center;margin:auto;"><?php //echo recaptcha_get_html($public_key); ?></div>-->
		<br /><input name="addBtn" type="submit" value="Sign Up">
    </div>
    </div>
	
	</form>
    <br /><br />
    
<?php
}
?>
<script type="text/javascript" src="js/signup.js"></script>

